/*
 *      Copyright 2008 Battams, Derek
 *       
 *       Licensed under the Apache License, Version 2.0 (the "License");
 *       you may not use this file except in compliance with the License.
 *       You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *       Unless required by applicable law or agreed to in writing, software
 *       distributed under the License is distributed on an "AS IS" BASIS,
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *       See the License for the specific language governing permissions and
 *       limitations under the License.
 */
package com.google.code.gwtsrwc.client;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RadioButton;

/**
 * An "on/off" widget that handles a binary decision when a checkbox isn't visually desirable
 * @author $Author: derek@battams.ca $
 * @version $Id: BinaryRadioButton.java 18 2008-09-07 13:38:27Z derek@battams.ca $
 * $URL: http://gwtsrwc.googlecode.com/svn/trunk/src/com/google/code/gwtsrwc/client/BinaryRadioButton.java $
 * 
 * <h3>CSS Style Rules</h3>
 * <ul>
 *    <li>.gwtsrwc-BinaryRadioButton { } # entire composite (containing panel)</li>
 *    <li>.gwtsrwc-BinaryRadioButton .gwt-RadioButton { } # radio buttons on composite</li>
 *    <li>.gwtsrwc-BinaryRadioButton-on { } # The "on" radio button</li>
 *    <li>.gwtsrwc-BinaryRadioButton-off { } # The "off" radio button</li>
 * </ul>
 * 
 */
public class BinaryRadioButton extends Composite {
	static private final String PRIMARY_STYLE_NAME = "gwtsrwc-BinaryRadioButton";
	static private final String ON_BUTTON = "gwtsrwc-BinaryRadioButton-on";
	static private final String OFF_BUTTON = "gwtsrwc-BinaryRadionButton-off";
	
	private Panel panel;
	private RadioButton on;
	private RadioButton off;
	
	/**
	 * Constructor
	 * @param group The name of the group to associate with the two radio buttons
	 * @param onLabel The text to use for the "on" button
	 * @param offLabel The text to use for the "off" button
	 * @param p The containing panel to hold the two radio buttons
	 */
	public BinaryRadioButton(String group, String onLabel, String offLabel, Panel p) {		
		panel = p;
		on = new RadioButton(group, onLabel);
		off = new RadioButton(group, offLabel);
		
		p.add(on);
		p.add(off);

		on.addStyleName(ON_BUTTON);
		off.addStyleName(OFF_BUTTON);
		
		initWidget(panel);
		setStylePrimaryName(PRIMARY_STYLE_NAME);		
	}
	
	/**
	 * Constructor; uses a HorizontalPanel as the container panel for the radio buttons
	 * @param group The name of the group to associate with the two radio buttons
	 * @param onLabel The text to use for the "on" button
	 * @param offLabel The text to use for the "off" button
	 */
	public BinaryRadioButton(String group, String onLabel, String offLabel) {
		this(group, onLabel, offLabel, new HorizontalPanel());
	}
	
	/**
	 * Set the state of the binary switch to "on"
	 */
	public void setOn() {
		on.setChecked(true);
		return;
	}
	
	/**
	 * Set the state of the binary switch to "off"
	 */
	public void setOff() {
		off.setChecked(true);
		return;
	}
	
	/**
	 * Set the state of the binary switch
	 * @param state The state to set the switch to; true is "on", false is "off"
	 */
	public void setState(boolean state) {
		if(state)
			setOn();
		else
			setOff();
		return;
	}
	
	/**
	 * Check if the switch is currently in the "on" state
	 * @return True if the current state is "on" or false if it's "off"
	 */
	public boolean isOn() {
		return on.isChecked();
	}
	
	/**
	 * Check if the switch is currently in the "off" state
	 * @return True if the current state is "off" or false if it's "on"
	 */
	public boolean isOff() {
		return off.isChecked();
	}

	/**
	 * Get the RadioButton widget representing the "on" switch for this widget instance
	 * @return The "on" RadioButton
	 */
	public RadioButton getOnButton() {
		return on;
	}
	
	/**
	 * Get the RadioButton widget representing the "off" switch for this widget instance
	 * @return The "off" RadioButton
	 */
	public RadioButton getOffButton() {
		return off;
	}
}